import {
  EndpointItem,
  EndpointParam,
  ServiceParam,
  ServiceItem,
  CreateParam,
  TaskParam,
  TaskItem,
  SampleItem,
  SampleTableItem,
  AnalyzeParams,
  AnalyzeItem,
  Children,
} from './model/profilingModel';
import { maintenanceHttp } from '../../../../utils/http/axios';
import { stampToDate } from '/@/utils/dateUtil';

enum Api {
  GET_SERVICE = '/v1/maintenance/apm/getAllServices',
  GET_ENDPOINT = '/v1/maintenance/apm/searchEndpoint',
  CREATE_TASK = '/v1/maintenance/apm/createProfileTask',
  GET_ALL_TASK_LIST = '/v1/maintenance/apm/getProfileTaskList',
  GET_SAMPLE_LIST = '/v1/maintenance/apm/getProfileTaskSegmentList',
  GET_TASK_DETAIL = '/v1/maintenance/apm/getProfiledSegment',
  GET_ANALYZE = '/v1/maintenance/apm/getProfileAnalyze',
}

//获取服务
export const getService = async (params: ServiceParam) => {
  const res = await maintenanceHttp.post<ServiceItem[]>({ url: Api.GET_SERVICE, params });
  const result = res.map((item) => {
    return {
      label: item.label,
      value: item.key,
    };
  });
  return result;
};
//获取端点
export const getEndpoint = async (params: EndpointParam) => {
  const res = await maintenanceHttp.post<EndpointItem[]>({ url: Api.GET_ENDPOINT, params });
  const result = res.map((item) => {
    return {
      label: item.label,
      value: item.key,
    };
  });
  return result;
};
//创建任务
export const createTask = async (params: CreateParam) => {
  const res = await maintenanceHttp.post<string>({ url: Api.CREATE_TASK, params });
  return res;
};
//获取任务列表
export const getTaskList = async (params: TaskParam) => {
  const res = await maintenanceHttp.post<TaskItem[]>({ url: Api.GET_ALL_TASK_LIST, params });
  res.map((item) => {
    item.startStr = stampToDate(item.startTime);
    item.logs.map((i) => {
      i.timeStr = stampToDate(i.operationTime);
      return i;
    });
    return item;
  });
  return res;
};
//获取采样列表
export const getSampleList = async (param: string) => {
  const _param = { taskID: param };
  const res = await maintenanceHttp.get<SampleItem[]>({ url: Api.GET_SAMPLE_LIST, params: _param });
  res.map((item) => {
    item.startStr = stampToDate(item.start);
    return item;
  });
  return res;
};

//采样表格
export const sampleTable = async (param: string) => {
  const _param = { segmentId: param };
  const res = await maintenanceHttp.get<SampleTableItem[]>({ url: Api.GET_TASK_DETAIL, params: _param });
  let sum = 0;
  res[res.length - 1].children = [];
  res.map((item) => {
    item.duration = item.endTime - item.startTime;
    sum = sum + item.duration;
    item.startStr = stampToDate(item.startTime);
    return item;
  });
  res.map((item) => {
    item.durationRate = Number((item.duration / sum).toFixed(4));
    res[res.length - 1].children?.push(item);
    return item;
  });
  res[res.length - 1].children?.pop();
  const result = [res[res.length - 1]];
  return result;
};

//获取分析表格
export const getAnalyze = async (params: AnalyzeParams) => {
  const res = await maintenanceHttp.post<AnalyzeItem>({ url: Api.GET_ANALYZE, params });
  const result = [];
  if (res.trees !== null) {
    const arr: Children[] = res.trees[0].elements;
    for (let i = 0; i < arr.length - 1; i++) {
      arr[i].children = [arr[i + 1]];
    }
    result.push(arr[0]);
  }
  return result;
};
